package cn.iocoder.yudao.module.erp.controller.admin.product;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductInventoryRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductInventorySaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductInventoryInfoDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductInventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 物料库存属性")
@RestController
@RequestMapping("/erp/product/productInventory")
@Validated
public class ErpProductInventoryController {

    @Resource
    private ErpProductInventoryService erpProductInventoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物料库存属性")
    @PreAuthorize("@ss.hasPermission('erp:product:create')")
    public CommonResult<Long> createErpProductInventory(@Valid @RequestBody ErpProductInventorySaveReqVO createReqVO) {
        return success(erpProductInventoryService.createErpProductInventory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料库存属性")
    @PreAuthorize("@ss.hasPermission('erp:product:update')")
    public CommonResult<Boolean> updateErpProductInventory(@Valid @RequestBody ErpProductInventorySaveReqVO updateReqVO) {
        erpProductInventoryService.updateErpProductInventory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料库存属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:product:delete')")
    public CommonResult<Boolean> deleteErpProductInventory(@RequestParam("id") Long id) {
        erpProductInventoryService.deleteErpProductInventory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料库存属性")
    @Parameter(name = "parentId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ErpProductInventoryRespVO> getErpProductInventory(@RequestParam("parentId") Long parentId) {
        ErpProductInventoryInfoDO erpProductInventory = erpProductInventoryService.getErpProductInventory(parentId);
        if(erpProductInventory == null){
            erpProductInventory = new ErpProductInventoryInfoDO();
            erpProductInventory.setFmaterialid(parentId);
        }
        return success(BeanUtils.toBean(erpProductInventory, ErpProductInventoryRespVO.class));
    }
}